//登录
import Login from '@/config/login.js';
//判断网络
const ifnetwork = function() {
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: function(res) {
				//没有网络
				if (res.networkType == 'none') {
					resolve(false);
				} else {
					resolve(true);
				}
			}
		})
	})
}
//全局授权
const globalauthorization = function() {
	if (!Cache.has('user_openid')) {
		uni.showModal({
			title: Lang.lang.authorisation,
			content: Lang.lang.authorisationdes,
			confirmText: Lang.lang.confirmtext,
			showCancel: false,
			success: function(res) {
				if (res.confirm) {
					Login.weixinglogin();
				}
			}
		})
	}
}
//当前年月日时间戳
const nowymd = function() {
	var date = new Date();
	var year = date.getFullYear(); //年
	var month = date.getMonth() + 1; //月
	var day = date.getDate(); //日
	var timeDate = year + "-" + month + "-" + day + " 0:0:0";
	return timeDate;
}
//“yyyy-MM-dd hh:mm:ss”转换成时间戳
const timeProcessing = function(timeDate) {
	let Time = new Date(timeDate);
	let timestemp = Time.getTime();
	return timestemp;
}

//时间倒计时
const updateCountdown = function(targetDate) {
	// 获取当前时间
	var currentDate = new Date();
	// 计算时间差
	var timeDiff = timeProcessing(targetDate) - timeProcessing(currentDate);
	// 转换时间差为倒计时格式
	var days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
	var hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
	var minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
	var seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
	// 更新倒计时显示
	var timestring = "";
	if (days > 0) {
		timestring += addzone(days) + "天";
	}
	if (hours > 0 || days > 0) {
		timestring += addzone(hours) + "时";
	}
	if (minutes > 0 || hours > 0 || days > 0) {
		timestring += addzone(minutes) + "分";
	}

	var updattime = timestring + addzone(seconds) + "秒";
}
//数字倒计时
const updatenumber = function(number) {
	console.log(number);
	if (number <= 0) {
		number = 0;
	} else {
		number--;
	}
	return number;
}
//加0
const addzone = function(number) {
	var numberstring = number;
	if (number <= 9) {
		numberstring = "0" + number;
	}
	return numberstring;
}
//打乱数组
const shuffle = function(arr) {
	var len = arr.length;
	for (var i = 0; i < len; i++) {
		var end = len - 1;
		var index = (Math.random() * (end + 1)) >> 0;
		var t = arr[end];
		arr[end] = arr[index];
		arr[index] = t;
	}
	return arr;
}
//获取随机值
const rand_value = function(arr) {
	var x = arr.length - 1;
	var y = 0;
	var randnum = parseInt(Math.random() * (x - y + 1) + y);
	return arr[randnum];
}
//获取随机值
const rand_number = function(number,min=0) {
	var x =number;
	var y =min;
	var randnum = parseInt(Math.random() * (x - y + 1) + y);
	return randnum;
}
export default {
	ifnetwork,
	globalauthorization,
	nowymd,
	timeProcessing,
	updateCountdown,
	shuffle,
	updatenumber,
	rand_value,
	rand_number,
}